//You are given an array prices where prices[i] is the price of a given stock on
// the ith day. 
//
// Find the maximum profit you can achieve. You may complete as many transaction
//s as you like (i.e., buy one and sell one share of the stock multiple times). 
//
// Note: You may not engage in multiple transactions simultaneously (i.e., you m
//ust sell the stock before you buy again). 
//
// 
// Example 1: 
//
// 
//Input: prices = [7,1,5,3,6,4]
//Output: 7
//Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 
//5-1 = 4.
//Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
//
// 
//
// Example 2: 
//
// 
//Input: prices = [1,2,3,4,5]
//Output: 4
//Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 
//5-1 = 4.
//Note that you cannot buy on day 1, buy on day 2 and sell them later, as you ar
//e engaging multiple transactions at the same time. You must sell before buying a
//gain.
// 
//
// Example 3: 
//
// 
//Input: prices = [7,6,4,3,1]
//Output: 0
//Explanation: In this case, no transaction is done, i.e., max profit = 0.
// 
//
// 
// Constraints: 
//
// 
// 1 <= prices.length <= 3 * 104 
// 0 <= prices[i] <= 104 
// 
// Related Topics 贪心算法 数组 
// 👍 1215 👎 0


package leetcode.editor.cn;
//Java：Best Time to Buy and Sell Stock II
 class P122BestTimeToBuyAndSellStockIi{
    public static void main(String[] args) {
        Solution solution = new P122BestTimeToBuyAndSellStockIi().new Solution();
        // TO TEST
    }
    //leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public int maxProfit(int[] prices) {
        //1:当前状况，money，1.买入money-price,2.卖出mongey+price,2.啥都不做money
        int[][] profit = new int[prices.length][2];
        //1.当前持有股票，1.昨天买的，今天没动，2.今天买的
        //2.没持有股票，2.昨天就没有，今天没买，2.今天卖了
        //1.第一天，profit[0][1]=-Inifinite
        profit[0][1] = -prices[0];
        profit[0][0] = 0;
        int max = 0;
        for (int i = 1; i < prices.length; i++) {
            int price = prices[i];
            profit[i][0] = Math.max(profit[i - 1][0], price + profit[i - 1][1]);
            profit[i][1] = Math.max(profit[i - 1][1], profit[i - 1][0] - price);

        }
        return profit[profit.length - 1][0];

    }
}
//leetcode submit region end(Prohibit modification and deletion)

}